/**
 * 
 */
package vo;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import util.HibernateUtil;

/**
 * @author Saulo
 * 
 */
public class CorDAO {

	// SALVAR COR
	public void salvarCor(CorVO cor) {

		Transaction transaction = null;

		Session session = HibernateUtil.currentSession();
		transaction = session.beginTransaction();

		session.saveOrUpdate(cor);

		transaction.commit();

	}

	// EXCLUIR COR
	public void excluir(Integer idCor) {
		Transaction transaction = null;

		CorVO cor = obter(idCor);

		Session session = HibernateUtil.currentSession();
		transaction = session.beginTransaction();

		session.delete(cor);

		transaction.commit();
	}

	// OBTER COR
	public CorVO obter(Integer idCor) {
		Transaction transaction = null;

		Session session = HibernateUtil.currentSession();
		transaction = session.beginTransaction();

		Criteria criteria = session.createCriteria(CorVO.class);
		criteria.add(Restrictions.eq("idCor", idCor));

		CorVO cor = (CorVO) criteria.uniqueResult();

		transaction.commit();

		return cor;
	}

	// CONSULTAR COR
	@SuppressWarnings("unchecked")
	public List<CorVO> consultarCores(CorVO corConsulta) {
		List<CorVO> listaDeCores = new ArrayList<CorVO>();

		Transaction transaction = null;

		Session session = HibernateUtil.currentSession();
		transaction = session.beginTransaction();

		Criteria criteria = session.createCriteria(CorVO.class);

		criteria.add(Restrictions.like("descriCor", corConsulta.getDescriCor()+ "%"));

		listaDeCores = criteria.list();

		transaction.commit();
		return listaDeCores;
	}

	// CONSULTAR TODAS AS CORES
	@SuppressWarnings("unchecked")
	public List<CorVO> consultarTodasCores() {
		List<CorVO> listaDeCores = new ArrayList<CorVO>();

		Transaction transaction = null;

		Session session = HibernateUtil.currentSession();
		transaction = session.beginTransaction();

		Criteria criteria = session.createCriteria(CorVO.class);

		listaDeCores = criteria.list();

		transaction.commit();
		return listaDeCores;
	}

	//RETORNA O NOME DA COR PARA AS PEQUISAS ONDE ESTAM AGREGADAS
	public CorVO consultarCorPorId(int id, Session session) {
		CorVO cor = new CorVO();
		Criteria criteria = session.createCriteria(CorVO.class).add(Restrictions.eq("idCor", id));
		cor = (CorVO) criteria.uniqueResult();
		return cor;
	}

}
